package database

import (
	"context"
	"fmt"
	"github.com/redis/go-redis/v9"
	"go-caipu/pkg/install/dto"
	"time"
)

var RedisClient *redis.Client

func InitRedisConn(config dto.Redis) error {
	RedisClient = redis.NewClient(&redis.Options{
		Addr:         fmt.Sprintf("%s:%s", config.Host, config.Port),
		Password:     config.Password,
		DB:           config.Db,
		DialTimeout:  5 * time.Second,
		ReadTimeout:  3 * time.Second,
		WriteTimeout: 3 * time.Second,
		PoolTimeout:  4 * time.Second,
	})
	_, err := RedisClient.Ping(context.Background()).Result()
	if err != nil {
		return err
	}
	return nil
}
